package com.example.libraryapp.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.appcompat.widget.TintTypedArray;
import androidx.appcompat.widget.Toolbar;

import com.example.libraryapp.R;


/**
 * Created by Ivan on 15/9/28.
 */
public class CnToolbar extends Toolbar {

    private View mView;
    private TextView mTextTitle;
    private EditText mSearchView;
    private ImageView mRightBtn;
    private ImageView mBackView;

    public CnToolbar(Context context) {
       this(context,null);
    }

    public CnToolbar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CnToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        if(attrs != null) {
            final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
                    R.styleable.CnToolbar, defStyleAttr, 0);

            boolean isShowSearchView = a.getBoolean(R.styleable.CnToolbar_isShowSearchView,false);
            boolean isShowBackView = a.getBoolean(R.styleable.CnToolbar_isShowBackView,false);
            if(isShowBackView) {
                setBackBtnVisibility(VISIBLE);
            }

            if(isShowSearchView) {
                showSearchView();
            }else {
                showTitleView();
            }
            a.recycle();
        }

    }

    private void initView() {
        if(mView == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            mView = inflater.inflate(R.layout.toolbar, this,true);
            mTextTitle = mView.findViewById(R.id.toolbar_title);
            mSearchView = mView.findViewById(R.id.toolbar_searchView);
            mRightBtn = mView.findViewById(R.id.toolbar_right_btn);
            mBackView = mView.findViewById(R.id.back_btn);
        }
    }

    public void setBackBtnVisibility(int visibility) {
        if(mBackView != null) {
            mBackView.setVisibility(visibility);
        }
    }

    public void setRightBtnVisibility(int visibility) {
        if(mRightBtn != null) {
            mRightBtn.setVisibility(visibility);
        }
    }

    public void setRightIcon(@DrawableRes int resourceId) {
        mRightBtn.setImageResource(resourceId);
        mRightBtn.setVisibility(VISIBLE);
    }

    public void setRightBtnOnClickListener(OnClickListener listener){
        mRightBtn.setOnClickListener(listener);
    }

    public void setBackBtnOnClickListener(OnClickListener listener){
        mBackView.setOnClickListener(listener);
    }

    public EditText getSearchView() {
        return mSearchView;
    }

    @Override
    public void setTitle(int resId) {
        setTitle(getContext().getText(resId));
    }

    @Override
    public void setTitle(CharSequence title) {
        initView();
        if(mTextTitle != null) {
            mTextTitle.setText(title);
            showTitleView();
        }
    }

    public void showSearchView() {
        if(mSearchView != null) {
            mSearchView.setVisibility(VISIBLE);
        }
        if (mTextTitle != null) {
            mTextTitle.setVisibility(GONE);
        }
    }

    public void showTitleView() {
        if(mTextTitle != null) {
            mTextTitle.setVisibility(VISIBLE);
        }
        if(mSearchView != null) {
            mSearchView.setVisibility(GONE);
        }
    }

}
